package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.utils.DateUtils;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizSearchKeywordUserHistory;
import cn.sfrog.project.biz.admin.domain.vo.BizSearchKeywordUserHistoryFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizSearchKeywordUserHistoryQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizSearchKeywordUserHistoryVO;
import cn.sfrog.project.biz.admin.mapper.BizSearchKeywordUserHistoryMapper;
import cn.sfrog.project.biz.admin.service.IBizSearchKeywordUserHistoryService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 用户搜索历史记录Service业务层处理
 */
@Service
public class BizSearchKeywordUserHistoryServiceImpl extends ServiceImpl<BizSearchKeywordUserHistoryMapper, BizSearchKeywordUserHistory> implements IBizSearchKeywordUserHistoryService {
    @Resource
    private BizSearchKeywordUserHistoryMapper bizSearchKeywordUserHistoryMapper;

    /**
     * 查询用户搜索历史记录
     *
     * @param id 用户搜索历史记录主键
     * @return 用户搜索历史记录
     */
    @Override
    public BizSearchKeywordUserHistoryVO selectBizSearchKeywordUserHistoryById(Long id) {
        return bizSearchKeywordUserHistoryMapper.selectBizSearchKeywordUserHistoryById(id);
    }

    /**
     * 查询用户搜索历史记录列表
     *
     * @param query 用户搜索历史记录
     * @return 用户搜索历史记录
     */
    @Override
    public List<BizSearchKeywordUserHistoryVO> selectBizSearchKeywordUserHistoryList(BizSearchKeywordUserHistoryQueryVO query) {
        return bizSearchKeywordUserHistoryMapper.selectBizSearchKeywordUserHistoryList(query);
    }

    /**
     * 新增用户搜索历史记录
     *
     * @param form 用户搜索历史记录
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizSearchKeywordUserHistory(BizSearchKeywordUserHistoryFormVO form) {
        BizSearchKeywordUserHistory bizSearchKeywordUserHistory = new BizSearchKeywordUserHistory();
        BeanUtils.copyProperties(form, bizSearchKeywordUserHistory);
        bizSearchKeywordUserHistory.setCreateTime(new Date());
        bizSearchKeywordUserHistory.setCreateBy(SecurityUtils.getUsername());
        bizSearchKeywordUserHistory.setUpdateTime(new Date());
        bizSearchKeywordUserHistory.setUpdateBy(SecurityUtils.getUsername());
        return bizSearchKeywordUserHistory.insert();
    }

    /**
     * 修改用户搜索历史记录
     *
     * @param form 用户搜索历史记录
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizSearchKeywordUserHistory(BizSearchKeywordUserHistoryFormVO form) {
        BizSearchKeywordUserHistory bizSearchKeywordUserHistory = new BizSearchKeywordUserHistory();
        BeanUtils.copyProperties(form, bizSearchKeywordUserHistory);
        bizSearchKeywordUserHistory.setUpdateTime(new Date());
        bizSearchKeywordUserHistory.setUpdateBy(SecurityUtils.getUsername());
        return bizSearchKeywordUserHistory.updateById();
    }

    /**
     * 批量删除用户搜索历史记录
     *
     * @param ids 需要删除的用户搜索历史记录主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizSearchKeywordUserHistoryByIds(Long[] ids) {
            return BizSearchKeywordUserHistory.DAO.delete(Wrappers.lambdaQuery(BizSearchKeywordUserHistory.class).in(BizSearchKeywordUserHistory::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return true;
    }

    @Override
    public Boolean insertOrUpdateSearchHistory(Boolean isUpdate,Long keywordId) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        if(!isUpdate){
            //关键字是第一次被搜索
            BizSearchKeywordUserHistory history = new BizSearchKeywordUserHistory();
            history.setUserId(userId);
            history.setKeywordId(keywordId);
            history.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserName());
            history.setUpdateTime(DateUtils.getNowDate());
            history.insert();
        }else {
            LambdaQueryWrapper<BizSearchKeywordUserHistory> lambdaQueryWrapper = new LambdaQueryWrapper<BizSearchKeywordUserHistory>()
                    .eq(BizSearchKeywordUserHistory::getUserId,userId)
                    .eq(BizSearchKeywordUserHistory::getKeywordId,keywordId);
            BizSearchKeywordUserHistory history = bizSearchKeywordUserHistoryMapper.selectOne(lambdaQueryWrapper);
            if(StringUtils.isNull(history)){
                BizSearchKeywordUserHistory bizSearchKeywordUserHistory = new BizSearchKeywordUserHistory();
                bizSearchKeywordUserHistory.setUserId(userId);
                bizSearchKeywordUserHistory.setKeywordId(keywordId);
                bizSearchKeywordUserHistory.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserName());
                bizSearchKeywordUserHistory.setUpdateTime(DateUtils.getNowDate());
                bizSearchKeywordUserHistory.insert();
            }else {
                history.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserName());
                history.setUpdateTime(DateUtils.getNowDate());
                history.updateById();
            }
        }
        return true;
    }

    @Override
    public List<String> selectAppUserSearchHistory() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return bizSearchKeywordUserHistoryMapper.selectAppUserSearchHistory(userId);
    }

    @Override
    public Boolean deleteUserSearchHistory(Long userId) {
        return bizSearchKeywordUserHistoryMapper.deleteUserSearchHistory(userId);
    }
}
